package top.lilele.adminSystem.web;

import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lilele.adminSystem.dto.IdParams;
import top.lilele.adminSystem.entity.AdminSpaceEntity;
import top.lilele.adminSystem.service.AdminSpaceService;
import top.lilele.adminSystem.utils.BaseResponse;
import top.lilele.adminSystem.utils.ResponseCodeEnum;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lilele
 */
@RestController
@RequestMapping(value = "space")
@Slf4j
@Api(tags = "空间管理")
public class AdminSpaceController {

    @Resource
    AdminSpaceService adminSpaceService;

    @ApiOperation(value = "添加我的空间文档")
    @SneakyThrows
    @PostMapping(value = "addMySpace")
    public BaseResponse add(@Validated @RequestBody AdminSpaceEntity adminSpaceEntity) {
        String id = StpUtil.getLoginIdAsString();
        adminSpaceEntity.setCreatedId(id);
        String documentId = adminSpaceService.add(adminSpaceEntity);
        return BaseResponse.info(ResponseCodeEnum.Success, documentId);
    }

    @ApiOperation(value = "查询我的空间文档列表")
    @SneakyThrows
    @PostMapping(value = "queryMySpaceList")
    public BaseResponse querySpaceList() {
        List list = adminSpaceService.querySpaceList();
        return BaseResponse.info(ResponseCodeEnum.Success, list);
    }

    @ApiOperation(value = "根据id查询我的空间文档信息")
    @SneakyThrows
    @PostMapping(value = "queryMySpaceById")
    public BaseResponse querySpaceById(@Validated @RequestBody IdParams idParams) {
        AdminSpaceEntity adminSpaceEntity = adminSpaceService.querySpaceById(idParams.getId());
        return BaseResponse.info(ResponseCodeEnum.Success, adminSpaceEntity);
    }

    @ApiOperation(value = "根据id更新我的空间文档信息")
    @SneakyThrows
    @PostMapping(value = "updateMySpaceById")
    public BaseResponse updateSpaceById(@Validated @RequestBody AdminSpaceEntity adminSpaceEntity) {
        adminSpaceEntity.setUpdatedId(StpUtil.getLoginIdAsString());
        adminSpaceService.updateSpaceById(adminSpaceEntity);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

}
